<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">广告名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="imgname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">图片类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="type" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">上/下线状态</label>
                            <div class="layui-input-inline">
                                <input type="text" name="status" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
							<button type="submit" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit  lay-filter="data-search-btn"><i class="layui-icon"></i>搜索</button>
							<button type="submit" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm" type="reset" ><i class="layui-icon">&#xe669;</i>重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn"> 添加图片信息 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn"> 删除用户 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<!-- <script src="../js/lay-config.js"></script> -->
<script src="../js/http.js"></script>
<script>
    layui.use(['form', 'table','laypage','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,//from表单
            table = layui.table,//表格
            layuimini = layui.layuimini;
			//加载数据
			//重置查询数据不刷新页面
		form.on('submit(submit-form)', function(data){
			var field = data.field;
			//执行操作
				return false;
        });
		//表单时间轴转换
		function DateFormat(sjc){  
			var date = new Date(sjc); 
			   var y = date.getFullYear();  
				var m = date.getMonth()+1;  
				m = m<10?'0'+m:m;   
				var d = date.getDate();    
				d = d<10?("0"+d):d;    
				var h = date.getHours();    
				h = h<10?("0"+h):h;    
				var min = date.getMinutes();    
				min = min<10?("0"+min):min;    
				var s = date.getSeconds();    
				s = s<10?("0"+s):s;    
				var str = y+"-"+m+"-"+d+" "+h+":"+min+":"+s;    
				return str;
		}

        table.render({
            elem: '#currentTableId',
            url: $api+'/img/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
				
            }],
			//sort：排序
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'id', width: 80, title: '序号', sort: true},
				{field: 'imgname', width: 100, title: '图片名称'},
                {field: 'imgsrc', title: '图片外联地址',width:120},
				{field: 'url', title: '链接地址', width:120},
                {field: 'background', width: 150, title: '背景颜色'},
				{field: 'timeon', width: 170, title: '开始时间', sort: true,templet:function(row){
					return DateFormat(row.timeon);
				}},
                {field: 'type', width: 80, title: '类型',templet:function(d){
					if(d.type==1){
						return "轮播图"
					}else{
						return "广告"
					}
				}},
				{field: 'status', width: 80, title: '状态',templet:function(d){
					if(d.status==1){
						return "上线"
					}else{
						return "下线"
					}
				}},
                {title: '操作', minWidth: 20, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });
		//条件查询 监听提交
        form.on('submit(data-search-btn)', function (data) {
            // var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
			var formData=data.field;//获得data数据
			var imgname = formData.imgname,      
				type=formData.type,
				status=formData.status,
				s1 = null;
			if(type=="轮播图"){	
				var type = s1,
				type = 1;
			}
			if(type=="广告"){
				var type = s1,
				type = 2;
			}
			if(status=="上线"){
				var status = s1,
				status = 1;
			}
			if(status=="下线"){
				var status = s1,
				status = 2;
			}

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
					//传参向后台
                    imgname:imgname,
					type:type,
					status:status
                },
				url:'http://localhost:8070/img/querylist',//后台条件查询接口路径
				method:'post'
            });

            return false;//阻止表单跳转 true:表单跳转
        });

        // 监听添加操作
        $(".data-add-btn").on("click", function () {

            var index = layer.open({
                title: '添加轮播图',
                type: 2,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: 'rotationImgadd.html',
				end:function(){
					//执行搜索重载
					table.reload('currentTableId');
				}
            });
            $(window).on("resize", function () {
                layer.full(index);
            });

            return false;
        });

        // 监听删除操作
        $(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'rotationImgedit.html',
					//用于获得当前DOM
					success:function(layero,index){
						//getChildFrame 获得打开中页面对象 拿到输入框
						var input = layer.getChildFrame('input',index);
						//根据下标选取输入框进行赋值
						input[0].value = obj.data.id;
						input[1].value = obj.data.imgname;
						input[2].value = obj.data.imgsrc;
						input[3].value = obj.data.background;
						input[4].value = obj.data.url;
						// input[5].value = DateFormat(obj.data.timeon);
						// input[4].value = obj.data.type;
						// input[5].value = obj.data.status;

					}
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
					//向后端发送ajax请求 POST请求 未使用resfil风格
					$.post("http://localhost:8070/img/deleteById?id="+obj.data.id,
						(res) => {
							//根据后台DtoUtil工具类errorCode返回的0说明删除成功
							if(res.code == 0){
								layer.alert("删除成功")
								obj.del();
								//关闭当前遮罩层
								layer.close(index);
							}else{
								layer.alert("程序发生以外，请重试")
							}
						})
                });
            }
        });


    });
</script>
<script>

</script>

</body>
</html>